/*
 * imx8_windows.c
 *
 *  Created on: 2024年3月12日
 *      Author: NI
 */

#include <vcapture/capture.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "imx8-windows.h"

#define LOG_ERROR 	printf


int initWin(screen_windows_dev_t * win_dev, int index){
	int rc = -1;
	int screen_ndisplays;
	screen_display_t *screen_disp;
	if (win_dev->display)
	{
		int i = 0;
		rc = screen_get_context_property_iv(*(win_dev->screen_ctx), SCREEN_PROPERTY_DISPLAY_COUNT, &screen_ndisplays);
		if (rc)
		{
			LOG_ERROR("screen_get_context_property_iv(SCREEN_PROPERTY_DISPLAY_COUNT)");
			return 1;
		}

		screen_disp = calloc(screen_ndisplays, sizeof(*screen_disp));
		if (screen_disp == NULL)
		{
			LOG_ERROR("could not allocate memory for display list");
			return 1;
		}

		rc = screen_get_context_property_pv(*(win_dev->screen_ctx), SCREEN_PROPERTY_DISPLAYS, (void **)screen_disp);
		if (rc)
		{
			LOG_ERROR("screen_get_context_property_ptr(SCREEN_PROPERTY_DISPLAYS)");
			free(screen_disp);
			return 1;
		}

		if (win_dev->display)
		{
			//int want_id = atoi(display);
			int want_id = win_dev->display;

			for (i = 0; i < screen_ndisplays; ++i)
			{
				int actual_id = 0; // invalid
				screen_get_display_property_iv(screen_disp[i], SCREEN_PROPERTY_ID, &actual_id);

				if (want_id == actual_id)
				{
					break;
				}
			}
		}
		if (i >= screen_ndisplays)
		{
			LOG_ERROR("couldn't find display %d", win_dev->display);
			free(screen_disp);
			return 1;
		}

		rc = screen_set_window_property_pv(win_dev->screen_win, SCREEN_PROPERTY_DISPLAY, (void **)&screen_disp[i]);
		if (rc)
		{
			LOG_ERROR("screen_set_window_property_ptr(SCREEN_PROPERTY_DISPLAY)");
			free(screen_disp);
			return 1;
		}
		free(screen_disp);
	}

	int val = SCREEN_USAGE_WRITE | SCREEN_USAGE_CAPTURE;
	rc = screen_set_window_property_iv(win_dev->screen_win, SCREEN_PROPERTY_USAGE, &val);
	if (rc)
	{
		LOG_ERROR("screen_set_window_property_iv(SCREEN_PROPERTY_USAGE)");
		return 1;
	}

	if (win_dev->pipeline > 0)
	{
		rc = screen_set_window_property_iv(win_dev->screen_win, SCREEN_PROPERTY_PIPELINE, &win_dev->pipeline);
		if (rc)
		{
			LOG_ERROR("screen_set_window_property_iv(SCREEN_PROPERTY_PIPELINE)");
			return 1;
		}
	}

//	int format = SCREEN_FORMAT_RGBA4444;
//	rc = screen_set_window_property_iv(win_dev->screen_win, SCREEN_PROPERTY_FORMAT, &format);
	rc = screen_set_window_property_iv(win_dev->screen_win, SCREEN_PROPERTY_FORMAT, &win_dev->format);
	if (rc)
	{
		LOG_ERROR("screen_set_window_property_iv(SCREEN_PROPERTY_FORMAT)");
		return 1;
	}

	rc = screen_set_window_property_iv(win_dev->screen_win, SCREEN_PROPERTY_FLIP, &win_dev->flip);
	if (rc)
	{
		LOG_ERROR("screen_set_window_property_iv(SCREEN_PROPERTY_FLIP)");
		return 1;
	}

	rc = screen_set_window_property_iv(win_dev->screen_win, SCREEN_PROPERTY_MIRROR, &win_dev->mirror);
	if (rc)
	{
		LOG_ERROR("screen_set_window_property_iv(SCREEN_PROPERTY_MIRROR)");
		return 1;
	}

	rc = screen_set_window_property_iv(win_dev->screen_win, SCREEN_PROPERTY_ROTATION, &win_dev->rotation);
	if (rc)
	{
		LOG_ERROR("screen_set_window_property_iv(SCREEN_PROPERTY_ROTATION)");
		return 1;
	}

	rc = screen_set_window_property_iv(win_dev->screen_win, SCREEN_PROPERTY_SIZE, win_dev->size);
	if (rc)
	{
		LOG_ERROR("screen_get_window_property_iv(SCREEN_PROPERTY_SIZE)");
		return 1;
	}

	rc = screen_set_window_property_iv(win_dev->screen_win, SCREEN_PROPERTY_POSITION, win_dev->pos);
	if (rc)
	{
		LOG_ERROR("screen_set_window_property_iv(SCREEN_PROPERTY_POSITION)");
		return 1;
	}

	rc = screen_set_window_property_iv(win_dev->screen_win, SCREEN_PROPERTY_BUFFER_SIZE, win_dev->buffer_size);
	if (rc)
	{
		LOG_ERROR("screen_set_window_property_iv(SCREEN_PROPERTY_BUFFER_SIZE)");
		return 1;
	}

	rc = screen_set_window_property_iv(win_dev->screen_win, SCREEN_PROPERTY_ZORDER, &win_dev->zorder);
	if (rc)
	{
		LOG_ERROR("screen_set_window_property_iv(SCREEN_PROPERTY_ZORDER)");
		return 1;
	}

	//int value = SCREEN_TRANSPARENCY_NONE;
	rc = screen_set_window_property_iv(win_dev->screen_win, SCREEN_PROPERTY_TRANSPARENCY, &win_dev->transparency);
	if (rc)
	{
		LOG_ERROR("screen_set_window_property_iv(SCREEN_PROPERTY_ZORDER)");
		return 1;
	}

	rc = screen_set_window_property_cv(win_dev->screen_win, SCREEN_PROPERTY_ID_STRING, strlen(win_dev->id_string), win_dev->id_string);
	if (rc)
	{
		LOG_ERROR("screen_set_window_property_cv(SCREEN_PROPERTY_ID_STRING)");
		return 1;
	}

	rc = screen_set_window_property_iv(win_dev->screen_win, SCREEN_PROPERTY_VISIBLE, &win_dev->visit);
	if (rc)
	{
		LOG_ERROR("screen_set_window_property_iv(SCREEN_PROPERTY_VISIBLE)");
		return 1;
	}

	rc = screen_create_window_buffers(win_dev->screen_win, win_dev->num_buffers);
	if (rc)
	{
		LOG_ERROR("screen_create_window_buffers");
		return 1;
	}

	rc = screen_get_window_property_iv(win_dev->screen_win, SCREEN_PROPERTY_RENDER_BUFFER_COUNT, &win_dev->render_buffer_count);
	if (rc)
	{
		LOG_ERROR("screen_get_window_property_iv(SCREEN_PROPERTY_RENDER_BUFFER_COUNT)");
		return 1;
	}

	if ((win_dev->render_buffers = calloc(win_dev->num_buffers, sizeof(screen_buffer_t))) == NULL
			|| (win_dev->pointers = calloc(win_dev->num_buffers, sizeof(void *))) == NULL
//			|| (saveBuff[i].frame_timestamp = calloc(nbufs, sizeof(uint64_t))) == NULL
//			|| (saveBuff[i].frame_nbytes = calloc(nbufs, sizeof(uint32_t))) == NULL
//			|| (saveBuff[i].frame_flags = calloc(nbufs, sizeof(uint32_t))) == NULL
//			|| (saveBuff[i].frame_seqno = calloc(nbufs, sizeof(uint32_t))) == NULL
			)
	{
		LOG_ERROR("calloc");
		return 1;
	}

	rc = screen_get_window_property_pv(win_dev->screen_win, SCREEN_PROPERTY_RENDER_BUFFERS, (void **)(win_dev->render_buffers));
	if (rc)
	{
		LOG_ERROR("screen_get_window_property_pv(SCREEN_PROPERTY_RENDER_BUFFERS)");
		return 1;
	}

	int i = 0;
	for (i = 0; i < win_dev->render_buffer_count; i++)
	{
		/* obtain the pointer of the buffers, for the capture use */
		rc = screen_get_buffer_property_pv(win_dev->render_buffers[i], SCREEN_PROPERTY_POINTER, &(win_dev->pointers[i]));
		if (rc)
		{
			LOG_ERROR("screen_get_window_property_pv(SCREEN_PROPERTY_POINTER)");
			return 1;
		}
	}

	rc = screen_get_buffer_property_iv(win_dev->render_buffers[0], SCREEN_PROPERTY_STRIDE, &win_dev->stride);
	if (rc)
	{
		LOG_ERROR("screen_get_buffer_property_iv(SCREEN_PROPERTY_STRIDE)");
		return 1;
	}


	return 0;
}
